#pragma once
#include "RBT.hpp"
#include <iostream>
#include <vector>

using std::vector;
using std::endl;
using std::cout;

namespace my_set
{
	using namespace my_rbt;

	template<typename K, typename Val>
	struct KeyOfVal
	{
		const K& operator()(const Val& val)
		{
			return val;
		}
	};

	template<typename K>
	class set
	{
	public:
		typedef typename RBTree<K, K, KeyOfVal<K, K>>::const_iterator  iterator;
		typedef typename RBTree<K, K, KeyOfVal<K, K>>::const_iterator const_iterator;
		typedef set<K> Self;
		typedef RBTree<K, K, KeyOfVal<K, K>> RBTree_;
		set()
		{}

		set(Self& m)
		{
			cout << "map(Self& m)" << endl;
			_rbt = m._rbt;
		}


		// 模板类中的&& 是 右值引用
		set(Self&& m)
		{
			cout << "map( Self&& m)" << endl;
			_rbt = std::forward<RBTree_>(m._rbt);

		}
		// const 对象 是不可能调用 operator =
		// const 成员函数，指的是在这个函数中不能修改类的成员变量
		Self& operator=(Self&& m)
		{
			cout << "Self& operator=(Self&& m)" << endl;
			_rbt = std::forward<RBTree_>(m._rbt);
			return *this;
		}
		Self& operator=(Self& m)
		{
			cout << "Self& operator=(Self& m)" << endl;
			_rbt = m._rbt;
			return *this;
		}


		std::pair<iterator, bool> insert(K kv)
		{
			return _rbt.insert(kv);

		}

		iterator find(const K& key)
		{
			auto res = _rbt.find(key);
			return res.first;
		}

		iterator begin()
		{
			return _rbt.begin();
		}

		iterator end()
		{
			return _rbt.end();
		}

		const_iterator begin() const
		{
			return _rbt.begin();
		}

		const_iterator end() const
		{
			return _rbt.end();
		}

	public:
		// map 中key是不能修改的
		RBTree_ _rbt;
	};
	void test_begin(const set<int>& st)
	{
		auto it = st.begin();
		while (it != st.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}
	void test_set()
	{
		set<int> st;
		vector<int> v({ 16, 3, 7, 11, 9, 26, 18, 14, 15 });
		for (int i = 0; i < v.size(); i++)
		{
			st.insert(v[i]);
		}
		auto it = st.begin();
		while (it != st.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
		cout << "-----------" << endl;
		test_begin(st);
	}
}